﻿function Runner() {
}

Runner.prototype = {
    login: function () {
        var userName = "";
        var password = "";

        var nsIRequest = _("nsIRequest");
        var requestFlags =
            nsIRequest.INHIBIT_PERSISTENT_CACHING |
                nsIRequest.LOAD_BYPASS_CACHE |
                    nsIRequest.LOAD_BACKGROUND;

        var authorization = Base64.encode(userName + ":" + password);
        var xhr = _("@mozilla.org/xmlextras/xmlhttprequest;1").instance("nsIXMLHttpRequest");
        xhr.open("GET", "http://www.neoseeker.com/members/login.html", false);
        xhr.setRequestHeader("Authorization", "Basic " + authorization);
        xhr.channel.loadFlags = requestFlags;
        xhr.send(null);
        var cookieHeader = xhr.getResponseHeader("Cookie");

        xhr.open("POST", "http://www.neoseeker.com/members/login.html", false);
        xhr.channel.loadFlags = requestFlags;
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
        xhr.setRequestHeader("Cookie", cookieHeader);

        var postData = "login_form=true&username=" + userName + "&password=" + password + "&submitbtn=Login+now";
        xhr.setRequestHeader("Authorization", "Basic " + authorization);
        xhr.send(postData);
    },

    logout: function () {
        var nsIRequest = _("nsIRequest");
        var bypassCache =
            nsIRequest.INHIBIT_PERSISTENT_CACHING |
                nsIRequest.LOAD_BYPASS_CACHE |
                    nsIRequest.LOAD_BACKGROUND;
        var xhr = null;

        // Logout is really flaky. Sometimes the tests still see us as logged
        // in even though we aren't. To counteract this, we are making two
        // requests, one to log out and one to confirm the log out. If the
        // log out isn't confirmed, we repeat the process.
        do {
            xhr = _("@mozilla.org/xmlextras/xmlhttprequest;1").instance("nsIXMLHttpRequest");
            xhr.open("GET", "http://www.neoseeker.com/members/logout.html", false);
            xhr.channel.loadFlags = bypassCache;

            xhr.send(null);
        } while (xhr.responseText.indexOf("Login</a></div>") === -1);
    },

    closeWindow: function () {
        var that = this;
        test("Close window", function() {
            that._window.close();
        });
    },

    navigateTo: function (uri) {
        var that = this;
        asyncTest("navigate to page", function () {
            that._window = window.open(uri);

            // For some reason we need a delay before subscribing to load in order for this to work.
            setTimeout(function () {
                $(that._window).on("load", function () {
                    var contentDocument = that._window.document;

                    $(contentDocument).ready(function () {
                        runner.$ = function (selector) {
                            return $(selector, contentDocument);
                        };

                        $(that._window).off("load", "**");
                        start();
                    });
                });
            }, 1000);
        });
    }
};

var runner = new Runner();